//
//  AccelerometerSensor.m
//  测试demo
//
//  Created by ycx on 2019/7/2.
//  Copyright © 2019 ycx. All rights reserved.
//

#import "AccelerometerSensor.h"


@interface AccelerometerSensor()
@property(nonatomic,strong)CMMotionManager * motionManager;
@property(nonatomic,strong)CMMotionActivityManager * activityManager;

@end


@implementation AccelerometerSensor
-(instancetype)init
{
    if(self == [super init])
    {
       // [self initialization];
    }
    return self;
}
-(void)initialization:(DeviceMotionBlock)block
{
//    if ([self.motionManager isDeviceMotionAvailable] && ![self.motionManager isDeviceMotionActive]) {
//        [self.motionManager startAccelerometerUpdates];
//        self.motionManager.accelerometerUpdateInterval= 0.1; //设定每0.1秒更新一次数据
//        //此方法每0.1秒就会返回一次 x y z数据
//       [self.motionManager startAccelerometerUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
//
//           if (error) {
//               [self.motionManager stopAccelerometerUpdates];
//               NSLog(@"There is something error for accelerometer update");
//           }else {
//               NSLog(@"\n加速度：\nX: %f\nY: %f\nZ: %f", accelerometerData.acceleration.x, accelerometerData.acceleration.y, accelerometerData.acceleration.z);
//           }
//
//        }];
//    }
//    else
//    {
//        NSLog(@"该设备不支持获取加速度数据！");
//    }
//
   
    
//    // 如果CMMotionManager的支持获取陀螺仪数据
//    if ([_motionManager isGyroAvailable] && ![_motionManager isGyroActive])
//    {
//        [self.motionManager startGyroUpdates];
//        self.motionManager.gyroUpdateInterval = 0.1;
//        [self.motionManager startGyroUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMGyroData * _Nullable gyroData, NSError * _Nullable error) {
//            if (error) {
//                [self stopGyroUpdate];
//                NSLog(@"There is something error for accelerometer update");
//            }else {
//                NSLog(@"\n旋转速度：\nX: %f\nY: %f\nZ: %f", gyroData.rotationRate.x, gyroData.rotationRate.y, gyroData.rotationRate.z);
//            }
//
//        }];
//    }
//    else
//    {
//        NSLog(@"该设备不支持获取陀螺仪数据！");
//    }
    
//    // 如果CMMotionManager的磁场数据可用
//   if ([self.motionManager isMagnetometerAvailable] && ![self.motionManager isMagnetometerActive])
//    {
//
//        [self.motionManager startMagnetometerUpdates];
//        self.motionManager.magnetometerUpdateInterval = 0.1;
//
//        [self.motionManager startMagnetometerUpdatesToQueue:[NSOperationQueue currentQueue] withHandler:^(CMMagnetometerData * _Nullable magnetometerData, NSError * _Nullable error) {
//
//            if (error) {
//                [self stopMagnetometerUpdate];
//                NSLog(@"There is something error for magnetometer update");
//            }else {
//                NSLog(@"\n磁力计：\nX: %f\nY: %f\nZ: %f", magnetometerData.magneticField.x, magnetometerData.magneticField.y, magnetometerData.magneticField.z);
//            }
//
////            空间位置的欧拉角，通过欧拉角可以算得手机两个时刻之间的夹角，比用角速度计算精确地多
////
////            double roll = motionManager.deviceMotion.attitude.roll;
////            double pitch = motionManager.deviceMotion.attitude.pitch;
////            double yaw = motionManager.deviceMotion.attitude.yaw;
//
//
////            空间位置的四元数（与欧拉角类似，但解决了万向结死锁问题）
////            double w = motionManager.deviceMotion.attitude.quaternion.w;
////            double wx = motionManager.deviceMotion.attitude.quaternion.x;
////            double wy = motionManager.deviceMotion.attitude.quaternion.y;
////            double wz = motionManager.deviceMotion.attitude.quaternion.z;
//
//        }];
//    }
    
    
    //判断设备运动传感器是否可用
    if ([self.motionManager isDeviceMotionAvailable] && ![self.motionManager isDeviceMotionActive]) {
        
        [self.motionManager startDeviceMotionUpdates];
        //实时获取数据
        [self.motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMDeviceMotion * _Nullable motion, NSError * _Nullable error) {
            
            if(!error)
            {
                if(block)
                {
                    block(motion);
                }
            }
            [self.motionManager stopDeviceMotionUpdates];
            
        }];
        
    }
 
}
-(void)stopDeviceMotion
{
      if ([self.motionManager isDeviceMotionAvailable] && ![self.motionManager isDeviceMotionActive]) {
          [self.motionManager stopDeviceMotionUpdates];
      }
}


-(CMMotionManager *)motionManager
{
    if(!_motionManager)
    {
        _motionManager = [[CMMotionManager alloc] init];
    }
    return _motionManager;
}



@end
